package cn.kow.kmall.system.service.impl;

import cn.kow.kmall.common.constant.KmallConstants;
import cn.kow.kmall.core.constant.KmallStatusConstant;
import cn.kow.kmall.core.exception.ServiceException;
import cn.kow.kmall.system.entity.SysMenu;
import cn.kow.kmall.system.entity.SysRole;
import cn.kow.kmall.system.entity.SysUser;
import cn.kow.kmall.admin.pojo.dto.MenuQueryDTO;
import cn.kow.kmall.system.repository.SysMenuRepository;
import cn.kow.kmall.system.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-10-29 18:27
 * @Modified By:
 */
@Slf4j
@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuRepository sysMenuRepository;

    @Override
    public void save(SysMenu menu) {
        if (null == menu.getPerms()) {
            menu.setPerms("");
        }
        if (null == menu.getParentId()) {
            menu.setParentId(0L);
        }
        if (0 == menu.getParentId()) {
            menu.setComponent("Layout");
        }
        if (StringUtils.isBlank(menu.getIcon())) {
            menu.setIcon("#");
        }
        sysMenuRepository.create(menu);
    }

    @Override
    public void remove(Long id) {
        sysMenuRepository.deleteById(id);
    }

    @Override
    public void update(SysMenu menu) {
        SysMenu sysMenu = sysMenuRepository.getById(menu.getId());
        Long parentId = menu.getParentId();
        if (0 == parentId) {
            sysMenu.setParentId(0L);
        } else {
            if (!parentId.equals(sysMenu.getParentId())) {
                SysMenu pMenu = getById(parentId);
                if (null == pMenu) {
                    throw new ServiceException(KmallConstants.FAIL, "上级菜单不存在");
                }
                if (!"M".equals(pMenu.getMenuType())) {
                    throw new ServiceException(KmallConstants.FAIL, "上级菜单类型错误");
                }
                sysMenu.setParentId(parentId);
            }
        }
        if (StringUtils.isNotEmpty(menu.getMenuType())) {
            sysMenu.setMenuType(menu.getMenuType());
        }
        if (StringUtils.isNotEmpty(menu.getMenuName())) {
            sysMenu.setMenuName(menu.getMenuName());
        }
        if (StringUtils.isNotEmpty(menu.getIcon())) {
            sysMenu.setIcon(menu.getIcon());
        } else {
            sysMenu.setIcon("#");
        }
        if (null != menu.getSort()) {
            sysMenu.setSort(menu.getSort());
        }
        if (null != menu.getIsFrame()) {
            sysMenu.setIsFrame(menu.getIsFrame());
        }
        if (null != menu.getVisible()) {
            sysMenu.setVisible(menu.getVisible());
        }
        if (null != menu.getPath()) {
            sysMenu.setPath(menu.getPath());
        }
        if (null != menu.getComponent()) {
            sysMenu.setComponent(menu.getComponent());
        }
        if (null != menu.getPerms()) {
            sysMenu.setPerms(menu.getPerms());
        }
        sysMenu.setUpdateBy(menu.getUpdateBy());
        sysMenuRepository.update(sysMenu);
    }

    @Override
    public SysMenu getById(Long id) {
        return sysMenuRepository.getById(id);
    }

    @Override
    public List<SysMenu> listByIds(List<Long> ids) {
        return sysMenuRepository.findAllById(ids);
    }


    @Override
    public List<SysMenu> listMenu(MenuQueryDTO queryDTO) {
        return sysMenuRepository.queryAll(queryDTO);
    }

    @Override
    public List<SysMenu> listMenuByUser(SysUser sysUser) {
        List<SysRole> roles = sysUser.getRoles();
        if (roles.stream().filter(r -> "admin".equals(r.getRoleCode())).findFirst().isPresent()) {
            MenuQueryDTO queryDTO = new MenuQueryDTO();
            queryDTO.setMenuType("M");
            return sysMenuRepository.queryAll(queryDTO);
        } else {
            List<SysMenu> list = new ArrayList<>();
            for (SysRole role : roles) {
                List<SysMenu> menus = role.getMenus();
                if (CollectionUtils.isEmpty(menus)) {
                    continue;
                }
                menus.stream().filter(m -> m.getVisible() == 1)
                        .filter(m -> Arrays.asList("M", "C").contains(m.getMenuType()))
                        .filter(m -> !m.getDeleted())
                        .filter(m -> m.getStatus().equals(KmallStatusConstant.NORMAL))
                        .sorted(Comparator.comparing(SysMenu::getSort))
                        .forEach(m -> list.add(m));
            }
            return list;
        }
    }
}
